package com.lanou.musicbox.adapter.seekmusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.Playlist;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/27.
 * 进入搜索_歌单页面Adapter
 */
public class EnterTheSeekMusic_PlaylistAdapter extends BaseAdapter{
    private List<Playlist.DataEntity> data;
    private Context mContext;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;

    public EnterTheSeekMusic_PlaylistAdapter(List<Playlist.DataEntity> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
        volleySingleton = VolleySingleton.getVolleySingleton(mContext);
        imageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return data != null && data.size() > 0 ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.entertheseekmusic_playlist_item, null);
            holder = new ViewHolder();
            holder.pic_url = (ImageView) view.findViewById(R.id.pic_url);
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.song_list = (TextView) view.findViewById(R.id.song_list);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Playlist.DataEntity p = data.get(i);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.pic_url, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(p.getPic_url(), listener);
        if (p != null) {
            holder.title.setText(p.getTitle());
            // 将String类型数据转换成数组以,为界线
            String[] data =  p.getSong_list().split(",");
            // 获取数组长度
            holder.song_list.setText(data.length+"");
        }
        return view;
    }

    private class ViewHolder {
        private ImageView pic_url;
        private TextView title, song_list;
    }
}
